package com.huawei.smartcity0102;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.huawei.smartcity0102.MainActivity;
import com.huawei.smartcity0102.util.Http;

/**
 * @author ZengYunyi
 * @description:
 * @date :2021/12/27 14:25
 */
public abstract class Base extends Fragment {
    View v, b;
    Boolean bottom;
    public ImageView back;
    public TextView title;
    public TextView right;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        b = inflater.inflate(R.layout.base, container, false);
        v = inflater.inflate(layId(), (ViewGroup) b, true);
        back = (ImageView) b.findViewById(R.id.back);
        title = (TextView) b.findViewById(R.id.title);
        right = (TextView) b.findViewById(R.id.right);
        if(title()==null){
            title.setVisibility(View.GONE);
        }else {
            title.setText(title());
        }
        if(back())back.setOnClickListener(v1->{remove();});
        else if(!back()) back.setVisibility(View.GONE);
        bottom=bottom();
        if(bottom==null){
            bottom=true;
            MainActivity.bottom.setVisibility(View.GONE);
        }else if(!bottom){
            MainActivity.bottom.setVisibility(View.GONE);
        }
        init(v);
        return b;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (bottom) MainActivity.bottom.setVisibility(View.VISIBLE);
    }

    public void add(Fragment fragment) {
        requireActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment)
                .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right).addToBackStack(getTag()).commit();
    }

    public void remove() {
        requireActivity().getSupportFragmentManager().popBackStack();
    }

    public void th(Runnable runnable) {
        new Thread(runnable).start();
    }

    public void ui(Runnable runnable) {
        requireActivity().runOnUiThread(runnable);
    }

    public void glide(ImageView ig, Object o) {
        if (o instanceof String) {
            Glide.with(ig.getContext()).load(Http.base + o).placeholder(R.drawable.addpic).error(R.drawable.addpic).into(ig);
        } else {
            Glide.with(ig.getContext()).load(o).placeholder(R.drawable.addpic).error(R.drawable.addpic).into(ig);
        }
    }

    public void toast(String name) {
        Toast.makeText(getContext(), name, Toast.LENGTH_SHORT).show();
    }

    protected abstract int layId();

    protected abstract String title();

    protected abstract Boolean back();

    protected abstract Boolean bottom();

    protected abstract void init(View iv);


}
